package cn.itlaobing.springmvc.web.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.itlaobing.springmvc.entity.Account;
import cn.itlaobing.springmvc.service.AccountService;

@Controller
public class SessionController extends BaseController {
	
	@Autowired
	private AccountService accountService;

	public static final String CURRENT_ACCOUNT="currentAccount";
	
	@RequestMapping(value={"/index","","/"})
	public String index(){
		return "index";
	}
	
	@RequestMapping(value={"/signin","/",""},method=RequestMethod.GET)
	public String signin(){
		return "signIn";
	}
	
	@RequestMapping(value={"/signin"},method=RequestMethod.POST)
	public String signin(String username,String userpass
			,HttpSession session
			,Model model){
		/*if(username.equals("admin") && userpass.equals("123456")) {
			session.setAttribute(CURRENT_ACCOUNT, username);
			return "redirect:/index";
		}
		model.addAttribute("toastMsg", "账号或密码错误");
		return "/signIn";*/
		
	Account account=accountService.validateAcc(username, userpass);
	if (account!=null) {
		session.setAttribute("name", account);
		return "redirect:/index";
	}
	return "signIn";	
	
	}
	
	
}
